<!-- Horizontal Form -->
<div id="page-wrapper">
 <div class="box box-info">
      <div class="box-header with-border">
        <h3 class="box-title">添加学生</h3>
      </div>
      <!-- /.box-header -->
      <!-- form start -->
      <form class="form-horizontal">
        <div class="box-body">
          <div class="form-group">
            <label for="nameId" class="col-sm-2 control-label">学生ID</label>
            <div class="col-sm-10">
              <input type="text" class="form-control"  id="studentID" placeholder="请输入id">
            </div>
          </div>
          <div class="form-group">
            <label for="nameId" class="col-sm-2 control-label">学生姓名</label>
            <div class="col-sm-10">
              <input type="text" class="form-control"  id="studentName" placeholder="请输入姓名">
            </div>
          </div>
         <div class="form-group">
            <label for="nameId" class="col-sm-2 control-label">性别</label>
            <div class="col-sm-10 typeRadio">
              	<label class="radio-inline">
				<input type="radio" name="genderId" value="男" checked> 男 </label>
				<label class="radio-inline">
				<input type="radio" name="genderId" value="女"> 女 </label>
            </div>
         </div>
          
          <div class="form-group">
            <label for="urlId" class="col-sm-2 control-label">老师</label>
            <div class="col-sm-10">
              <input type="text" class="form-control"  id="teacher" placeholder="">
            </div>
          </div>
          <div class="form-group">
            <label for="urlId" class="col-sm-2 control-label">手机号</label>
            <div class="col-sm-10">
              <input type="text" class="form-control"  id="mobile" placeholder="请输入手机号">
            </div>
          </div>
          <div class="form-group">
			<label for="permissionId" class="col-sm-2 control-label">邮箱</label>
			<div class="col-sm-10">
              <input type="text" class="form-control"  id="email" placeholder="请输入邮箱名称">
            </div>
		  </div>
          <div class="form-group">
			<label for="permissionId" class="col-sm-2 control-label">用户名</label>
			<div class="col-sm-10">
              <input type="text" class="form-control"  id="username" placeholder="请输入用户名">
            </div>
		  </div>
          <div class="form-group">
			<label for="permissionId" class="col-sm-2 control-label">密码</label>
			<div class="col-sm-10">
              <input type="text" class="form-control"  id="password" placeholder="请输入密码">
            </div>
		  </div>
		  
        </div>
        <!-- /.box-body -->
        <div class="box-footer">
          <button type="button" class="btn btn-default btn-cancel">Cancel</button>
          <button type="button" class="btn btn-info pull-right btn-save">Save</button>
        </div>
        <!-- /.box-footer -->
      </form>
  </div>
</div>
<!--   <script type="text/javascript" src="bower_components/ztree/jquery.ztree.all.min.js"></script>
  <script type="text/javascript" src="bower_components/layer/layer.js">
  </script> -->
  <script type="text/javascript">
  
  $(function(){  
	  //box-footer中按钮事件注册
	  $(".box-footer")
	  .on("click",".btn-cancel",doCancel)
	  .on("click",".btn-save,.btn-update",doSaveOrUpdate)
	  
	  //获取mainContentId上值
	  var rowData=
	  $("#mainContentId").data("rowData");
	  //假如有值说明是修改,则基于此值初始化编辑页面.
	  if(rowData)doInitEditFormData(rowData);
  });
  //初始化表单数据
  function doInitEditFormData(rowData){
	$("input[value="+rowData.gender+"]").prop("checked",true);
	$("#studentId").val(rowData.id);  
	$("#studentName").val(rowData.studentName);  
	$("#teacher").val(rowData.teacher);  
	$("#mobile").val(rowData.mobile);  
	$("#email").val(rowData.email);  
	$("#username").val(rowData.username);  
	$("#password").val(rowData.password); 
  }
  //回到列表页面
  function doCancel(){
	var url="student/student_list";
	$("#mainContentId").load(url);  
  }
  //将表单数据发送到服务端
  function doSaveOrUpdate(){
	  //debugger
	 //1.获取表单中用户输入数据
	 var params=doGetEditFormData();
	 var rowData=$("#mainContentId").data("rowData");
	 if(rowData)params.id=rowData.id;
	 //2.异步提交数据到服务端
	 var insertUrl="student/doSaveStudentObject";
	 var updateUrl="student/doUpdateStudentObject";
	 var url=rowData?updateUrl:insertUrl; 
	 $.post(url,params,function(result){
		 if(result.state==1){
			 alert(result.message);
			 doCancel();
		 }else{
			 alert(result.message);
		 }
	 })
  }
  //获取修改的学生参数
  function doGetEditFormData(){
	  var params={
	     "id":$("#studentId").val(),
	     "studentName":$("#studentName").val(),
	     "gender":$("input[name='genderId']:checked").val(),
	     "mobile":$("#mobile").val(),
	     "teacher":$("#teacher").val(),
	     "email":$("#email").val(),
	     "username":$("#username").val(),
	     "password":$("#password").val()
	  }
	  return params;
  }
 </script>
    
    
    
    
    
    
    
    
    